package com.mineplatform.service;

import java.util.Date;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mineplatform.dao.M_FeedbackDao;
import com.mineplatform.dao.M_TokenDao;
import com.mineplatform.dao.M_UserDao;
import com.mineplatform.domain.M_Feedback;
import com.mineplatform.domain.M_Token;
import com.mineplatform.domain.M_User;
import com.mineplatform.exception.FeedbackNullOrEmptyException;
import com.mineplatform.exception.TokenDisableException;
import com.mineplatform.exception.TokenNotExistException;
import com.mineplatform.exception.TokenNullOrEmptyException;
import com.mineplatform.exception.UserNotExistException;
import com.mineplatform.iservice.IM_FeedbackService;
import com.mineplatform.util.CONSTANT;

@Service
public class M_FeedbackService implements IM_FeedbackService {

	private final M_FeedbackDao _m_feedbackdao;
	private final M_TokenDao _m_tokendao;
	private final M_UserDao _m_userdao;

	@Autowired
	public M_FeedbackService(M_FeedbackDao m_feedbackdao, M_TokenDao m_tokendao, M_UserDao m_userdao) {
		this._m_feedbackdao = m_feedbackdao;
		this._m_tokendao = m_tokendao;
		this._m_userdao = m_userdao;
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void feedbackanonymous(String feedback) throws FeedbackNullOrEmptyException {

		if (feedback == null) {

			throw new FeedbackNullOrEmptyException();
		}

		if (feedback.trim() == "") {
			throw new FeedbackNullOrEmptyException();
		}

		// TODO Auto-generated method stub
		M_Feedback back = new M_Feedback();
		String newId = UUID.randomUUID().toString();
		back.setId(newId);
		back.setCreateTime(new Date());
		back.setAnonymous(true);
		back.setUserId(null);
		back.setTitle(null);
		back.setFeedBack(feedback);
		back.setType(CONSTANT.FEEDBACK_DEFINE.SUGGESTION);

		this._m_feedbackdao.save(back);

	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void feedback(String token, String feedback) throws FeedbackNullOrEmptyException, TokenNullOrEmptyException,
			TokenNotExistException, TokenDisableException, UserNotExistException {
		// TODO Auto-generated method stub
		if (feedback == null) {

			throw new FeedbackNullOrEmptyException();
		}

		if (feedback.trim() == "") {
			throw new FeedbackNullOrEmptyException();
		}

		if ((token == null) || (token.equals(""))) {
			throw new TokenNullOrEmptyException();
		}

		M_Token tokenModel = this._m_tokendao.findByToken(token);
		if (tokenModel == null) {
			throw new TokenNotExistException();
		}

		if (!tokenModel.getEnable()) {
			throw new TokenDisableException();
		}

		M_User user = this._m_userdao.findById(tokenModel.getUserId());
		if (user == null) {
			throw new UserNotExistException();
		}

		// TODO Auto-generated method stub
		M_Feedback back = new M_Feedback();
		String newId = UUID.randomUUID().toString();
		back.setId(newId);
		back.setCreateTime(new Date());
		back.setAnonymous(false);
		back.setUserId(user.getId());
		back.setTitle(null);
		back.setFeedBack(feedback);
		back.setType(CONSTANT.FEEDBACK_DEFINE.SUGGESTION);

		this._m_feedbackdao.save(back);

	}

}
